package anc.tankshooter.ui;

import java.util.ArrayList;

import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;

import anc.tankshooter.GameActivity;
import anc.tankshooter.database.HighScore;
import anc.tankshooter.network.ui.MatchListScreen;
import anc.tankshooter.ui.ConfirmScreen.ButtonDialogWatcher;
import anc.tankshooter.ui.SceneManager.SceneType;

public class MainMenuScreen extends IScreen {

	private Sprite mLogoSprite;
	private Sprite mBackGround;
	private AnimatedSprite[] mMenus; 
	
	@Override
	protected void createScene() {
		
		mLogoSprite= new Sprite(205,100,mResourcesManager.mLogoColorTexture, mVertextManager);
		mMenus = new AnimatedSprite[5];
		mBackGround = new Sprite(0, 0, ResourcesManager.getInstance().mMenuBackgroundTexture, mVertextManager);
		mMenus[0] = new AnimatedSprite(275, 220, ResourcesManager.getInstance().mMainMenuTexture, mVertextManager){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP){
					this.setCurrentTileIndex(0);
					doSinglePlayer();
					
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN) {
					this.setCurrentTileIndex(1);
				}else this.setCurrentTileIndex(0);
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		
		mMenus[1] = new AnimatedSprite(275, 259, ResourcesManager.getInstance().mMainMenuTexture, mVertextManager){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP){
					this.setCurrentTileIndex(2);
					doHighScore();
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN) {
					this.setCurrentTileIndex(3);
				}else this.setCurrentTileIndex(2);
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		
		mMenus[2] = new AnimatedSprite(275, 298, ResourcesManager.getInstance().mMainMenuTexture, mVertextManager){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP){
					this.setCurrentTileIndex(4);
					doMultiPlayer();
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN) {
					this.setCurrentTileIndex(5);
				}else this.setCurrentTileIndex(4);
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		
		mMenus[3] = new AnimatedSprite(275, 337, ResourcesManager.getInstance().mMainMenuTexture, mVertextManager){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP){
					this.setCurrentTileIndex(6);
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN) {
					this.setCurrentTileIndex(7);
				}else this.setCurrentTileIndex(6);
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		
		mMenus[4] = new AnimatedSprite(275, 376, ResourcesManager.getInstance().mMainMenuTexture, mVertextManager){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP){
					this.setCurrentTileIndex(8);
					mActivity.finish();
					System.exit(0);
				}else if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN) {
					this.setCurrentTileIndex(9);
				}else this.setCurrentTileIndex(8);
				return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
			}
		};
		
		
		this.attachChild(mBackGround);
		this.attachChild(mLogoSprite);
		for(int i=0;i<mMenus.length;i++){
			mMenus[i].setCurrentTileIndex(i*2);
			this.registerTouchArea(mMenus[i]);
			this.attachChild(mMenus[i]);
			
		}
	}

	private void doSinglePlayer(){
		ResourcesManager.getInstance().loadLevelSelectionResources();
		PickLevelScreen sc=new PickLevelScreen();
		SceneManager.getInstance().setScene(sc);
		
	}
	
	private void doHighScore()
	{
		HighScoreScreen hss = new HighScoreScreen(false, new ButtonDialogWatcher() {
			
			@Override
			public void onOkClick() {
				MainMenuScreen.this.clearChildScene();
			}
			
			@Override
			public void onCancleClick() {
				// TODO Auto-generated method stub
				
			}
		});
		setChildScene(hss, false, false, true);
		ArrayList<HighScore> hs = GameActivity.mDatabase.getHighScores(10);
		hss.setHighScoreData(hs);
	}
	private void doMultiPlayer(){
		MatchListScreen sc = new MatchListScreen();
		SceneManager.getInstance().setScene(sc);
	}
	
	

	@Override
	public boolean onBackKeyPressed() {
		return false;
	}

	@Override
	public SceneType getSceneType() {
		return SceneType.SCENE_MENU;
	}

	@Override
	public void disposeScene() {
		mBackGround.detachSelf(); mBackGround.dispose();
		mLogoSprite.detachSelf(); mLogoSprite.dispose();
		for (int i = 0; i < mMenus.length; i++) {
			mMenus[i].detachSelf();
			mMenus[i].dispose();
			
		}
	}

	@Override
	public void onUpdateScreen(float second) {
		// TODO Auto-generated method stub
		
	}

}

